package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.mapper.RoleMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    public void deleteById(Long id) {
        roleMapper.deleteRelation(id);
        roleMapper.deletebymenu(id);
        roleMapper.deleteByPrimaryKey(id);
    }

    public void updateOrSave(Role role, Long[] ids,Long[] menuIds) {
        roleMapper.deleteRelation(role.getId());
        roleMapper.deletebymenu(role.getId());
        if (role.getId() != null) {
            roleMapper.updateByPrimaryKey(role);
        } else {
            roleMapper.insert(role);
        }
        if (ids!=null){

            for (Long Pid :ids){
                roleMapper.insertRelation(role.getId(),Pid);
            }
            for (Long menuId :menuIds){
                roleMapper.insertmune(role.getId(),menuId);
            }
        }
    }

    public Role getById(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        Integer totalCount = roleMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getPageSize());
        }
        List<Role> data = roleMapper.queryForList(qo);
        return new PageResult(totalCount,data,qo.getCurrentPage(),qo.getPageSize());
    }
}
